package com.can.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.can.reggie.common.BaseContext;
import com.can.reggie.common.R;
import com.can.reggie.entity.ShoppingCart;
import com.can.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        //查询当前菜品或者套餐,在购物车是否重复
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //添加到购物车的是菜品,则添加对应条件
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

            //key 这里条件不添加口味的判断了,因为前端页面没有展示,只能添加添加相同口味的产品
            /*

            if (shoppingCart.getDishFlavor() != null) {
                //口味不为空
                queryWrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
            } else {
                //口味为空
                queryWrapper.isNull(ShoppingCart::getDishFlavor);
            }
            */
        } else {
            //添加到购物车的是套餐,则添加对应条件
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //key 这里条件不添加口味的判断了,因为前端页面没有展示,只能添加添加相同口味的产品
        /*
       SQL:select * from shopping_cart where user_id = ? and dish_id = ? and dish_flavor and dish_flavor = ?
       SQL:select * from shopping_cart where user_id = ? and setmeal_id = ? and dish_flavor is null
        */

        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and setmeal_id = ?/setmeal_id = ?
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartOne != null) {
            //如果已经存在，就在原来数量基础上加一
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCartOne);

        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCartService.save(shoppingCart);
            shoppingCartOne = shoppingCart;
        }

        return R.success(shoppingCartOne);
    }


    /**
     * 购物车数量-1
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);


        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        //SELECT * FROM shopping_cart WHERE (user_id = ? AND dish_id = ?/getSetmealId = ?)
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartServiceOne.getNumber()==1){
            //数量为1,则删除,同时返回数量为0的ShoppingCart
            shoppingCartService.removeById(shoppingCartServiceOne.getId());
            shoppingCartServiceOne.setNumber(0);
            return R.success(shoppingCartServiceOne);
        }else {
            //数量不为1 , 则-1 ,并返回
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartServiceOne);
            return R.success(shoppingCartServiceOne);
        }

    }


    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }


    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }


}



